package com.leetcode.根据算法进行分类.滑动窗口相关;

/**
 * @author: xiaomi
 * @date: 2021/2/3
 * @description: 1004. 最大连续1的个数 III
 * https://leetcode-cn.com/problems/max-consecutive-ones-iii/
 */
public class B_1004_最大连续1的个数_III {

	public static void main(String[] args) {

	}

	public int longestOnes(int[] A, int K) {
		int len = A.length;
		int left = 0, right = 0;
		int maxCount = 0;
		int res = 0;

		while (right < len) {
			if (A[right] == 1) {
				maxCount++;
			}
			right++;
			if (maxCount + K < right - left) {
				if (A[left] == 1) {
					maxCount--;
				}
				left++;
			}

			res = Math.max(res, right - left);
		}
		return res;
	}

	public int longestOnes2(int[] A, int K) {
		int len = A.length;
		int left = 0, right = 0;
		int maxCount = 0;
		int res = 0;

		while (right < len) {
			if (A[right] == 1) {
				maxCount++;
			}
			right++;
			if (right - left > K + maxCount) {
				//如果此时 区间 大于要求的范围
				if (A[left] == 1) {
					maxCount--;
				}
				left++;
			}
			res = Math.max(res, right - left);
		}
		return res;
	}

}
